﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using Co9320.NamaAlert.Server.Parameters;
using Co9320.NamaAlert.Shared;

namespace Co9320.NamaAlert.Server.BackEnd
{
	internal abstract class ProcessObject:IDisposable
	{
		private class EmptyProcess : ProcessObject
		{
			public EmptyProcess():base()
			{
				
			}

			protected override void Dispose(bool Disposing)
			{
			}

			public override void DoProcess()
			{
				return;
			}
		}

		public static ProcessObject NoProcessableObject = new EmptyProcess();

		
		internal static readonly string ConnectionString = Properties.Settings.Default.ConnectionStrings;
		protected static readonly int MaxCharacterLength = Properties.Settings.Default.MaxCharacterLength;

		private SqlConnection con;
		protected SqlCommand cmd;
		protected SqlParameter sid;
		protected SqlParameter cstr;
		private LiveInfo info_;

		protected ProcessObject(CommonInformation Parameter)
		{
			if (Parameter == null)
			{
				throw new ArgumentNullException("Parameter");
			}

			if (Parameter.Caster == null || Parameter.StationId == null)
			{
				throw new ArgumentException();
			}

			con = new SqlConnection(ConnectionString);
			con.Open();

			cmd = new SqlCommand();
			sid = new SqlParameter("@sid", System.Data.SqlDbType.NVarChar);
			cstr = new SqlParameter("@cstr", System.Data.SqlDbType.NVarChar);
			cmd.Parameters.Add(sid);
			cmd.Parameters.Add(cstr);

			sid.Value = Parameter.StationId;
			cstr.Value = Parameter.Caster;


			cmd.Connection = con;

			this.info_=new LiveInfo(Parameter.StationId,Parameter.Caster);
		}

		private ProcessObject()
		{
			con = null;
			cmd = null;
			info_ = null;
		}

		public abstract void DoProcess();

		public LiveInfo Info
		{
			get
			{
				return this.info_;
			}
		}

		protected static void TrimValue(System.Collections.IList List, int MaxTransaction)
		{
			if (List.Count > MaxTransaction)
			{
				for (int i = MaxTransaction; i < List.Count; i++)
				{
					List.RemoveAt(MaxTransaction);
				}
			}
		}


		#region IDisposable メンバ

		void IDisposable.Dispose()
		{
			Dispose(true);
		}

		protected virtual void Dispose(bool Disposing)
		{
			if (Disposing)
			{
				con.Dispose();
				cmd.Dispose();
			}

			GC.SuppressFinalize(this);
		}

		#endregion
	}


}
